//全局js.每个都要引入
info();
function info() {
    var data=urldata('post','/user/info',undefined)
    if(data.code!==0){
        location.href="/page/login.html"
    }
}
//退出登录
function loginout() {
    var data=urldata('post','/login/userLogout',undefined)
    localStorage.removeItem('token')
    window.location.reload();
}
function urldata(requestObject,url,data) {
    var token=localStorage.getItem('token');
    var data1='';
    $.ajax({
        type: requestObject,
        url: url,
        headers: {
            'Authorization': token,
            'Content-Type': 'application/json',
        },
        data:data,
        async:false,
        success: function(data) {
            if(data.code!==undefined && data.code===402){
                location.href="/page/login.html"
            }
            data1=data;
        }
    })
    return data1
}
function urlFromData(requestObject,url,data) {
    var token=localStorage.getItem('token');
    var data1='';
    $.ajax({
        type: requestObject,
        url: url,
        headers: {
            'Authorization': token,
        },
        processData: false,
        contentType: false,
        dataType: "json",
        data:data,
        async:false,
        success: function(data) {
            if(data.code!==undefined && data.code===402){
                location.href="/page/login.html"
            }
            data1=data;
        }
    })
    return data1
}
//公共的js
api ={
    //获取当前时间
    GetDate:function () {
        var date=new Date()
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h=h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        minute = minute < 10 ? ('0' + minute) : minute;
        var second=date.getSeconds();
        second=second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
    },
    GET: function(url, data, callback) {
        axios({
            method: 'get',
            url: url,
            params: data,
            headers: {
                'Authorization': localStorage.getItem('token'),
                // 'Redirect-Header': false,
                'Content-Type': 'application/json'
            }
        }).then(function(response) {
            if (response.status == 200) {
                callback(response.data)
            } else {
                errorTip(response.data.message)
            }
        }).catch(function(error) {
            console.log(error)
        })
    },
    POST: function(url, data, callback) {
        axios({
            method: 'post',
            url: url,
            data: JSON.stringify(data),
            headers: {
                'Authorization': localStorage.getItem('token'),
                //   'Redirect-Header': false,
                'Content-Type': 'application/json',
            }
        }).then(function(response) {
            if (response.status == 200) {
                callback(response.data)
            } else {
                errorTip(response.data.message)
            }
        }).catch(function(error) {
            console.log(error)
        })
    },
    POSTData: function(url, data, callback) {
        axios({
            method: 'post',
            url: url,
            data: data,
            headers: {
                'Authorization': localStorage.getItem('token'),
                //  'Redirect-Header': false
            }
        }).then(function(response) {
            if (response.status == 200) {
                callback(response.data)
            } else {
                errorTip(response.data.message)
            }
        }).catch(function(error) {
            console.log(error)
        })
    },
    PUT: function(url, data, callback) {
        axios({
            method: 'put',
            url: url,
            data: data,
            headers: {
                'Authorization': localStorage.getItem('token'),
                //  'Redirect-Header': false,
            }
        }).then(function(response) {
            if (response.status == 200) {
                callback(response.data)
            } else {
                errorTip(response.data.message)
            }
        }).catch(function(error) {
            console.log(error)
        })
    },
    DELETE: function(url, data, callback) {
        axios({
            method: 'post',
            url: base+url+'/delete',
            data: JSON.stringify(data),
            headers: {
                'Authorization': localStorage.getItem('token'),
                // 'Redirect-Header': false,
                'Content-Type': 'application/json'
            }
        }).then(function(response) {
            if (response.status == 200) {
                callback(response.data)
            } else {
                errorTip(response.data.message)
            }
        }).catch(function(error) {
            console.log(error)
        })
    },
    URLAjax :function (requestObject,url,data) {
        var data1='';
        $.ajax({
            type: requestObject,
            url: url,
            data:data,
            headers: {
                'Authorization':localStorage.getItem('token')
            },
            async:false,
            success: function(data) {
                data1=data;
            }
        })
        return data1
    },
    //将从form中通过$('#refer').serialize()获取的值转成json
    formToJson: function (data) {
        data=data.replace(/&/g,"\",\"");
        data=data.replace(/=/g,"\":\"");
        data="{\""+data+"\"}";
        return data;
    }
}
